const Mongoose = require("mongoose");
// 先规定好schema,然后再定义model
const Schema = Mongoose.Schema;
const UserSchema = new Schema({
  username: {
    type: String,
    unique: true,
    required: true
  },
  password: {
    type: String,
    set: val => {
      // 对密码进行加密
      return require("bcryptjs").hashSync(val, 10);
    },
    required: true
  },
  email: {
    type: String,
    required: true
  },
  createTime: {
    type: String,
    // 默认值为一个时间戳
    default: () => {
      return new Date().toLocaleString();
    }
  }
});

const UserModel = Mongoose.model("User", UserSchema);

module.exports = UserModel;
